﻿using JXC.EFCore;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JXC.Core.Models;
using Microsoft.EntityFrameworkCore;
using JXC.Core.Repository;
using AutoMapper;
using JXC.Application.Services.UserInfoService;
using JXC.Application.Dtos;
using JXC.Utility;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 权限管理控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserInfoController : BaseController
    {
        private readonly AppDbContext _context;
        private readonly IRepository<UserInfos, Guid> _repository;
        private readonly IMapper _mapper;
        private readonly IUserInfoService _userInfoService;
        private readonly IRepository<RoleInfos, int> _repositoryRole;
        private readonly IRepository<UserRoleInfos, int> _repositoryuserRole;

        public UserInfoController(AppDbContext context, IRepository<UserInfos, Guid> repository, IMapper mapper, IUserInfoService userInfoService, IRepository<RoleInfos, int> repositoryRole,IRepository<UserRoleInfos,int>repositoryuserRole)
        {
            _context = context;
            _repository = repository;
            _mapper = mapper;
            _userInfoService = userInfoService;
            _repositoryRole = repositoryRole;
            _repositoryuserRole = repositoryuserRole;
        }
        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        // GET: api/<UserInfoController>
        [HttpGet]
        public IEnumerable<UserInfos> GetAll()
        {
            return _context.UserInfos.ToList();
        }

        /// <summary>
        /// 根据用户id 获取所有的角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //public async Task<IActionResult> GetRolesByUserId(Guid id)
        //{

        //}

        //获取所有的用户列表
        [HttpPost]
        public async Task<IActionResult> GetUserInfoList([FromBody] UserInfoDto userInfos)
        {
            var list = await _userInfoService.GetPagedResult(userInfos);
            //var list = await _repository.GetAllListAsync();
            return Ok(list.Data);

        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [Route("AddUserInfo")]
        [HttpPost]
        public async Task<IActionResult> AddUserInfo(UserInfoDtos userInfo)
        {
            UserInfos user = await _repository.InsertAsync(_mapper.Map<UserInfos>(userInfo));
            if (user == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(AjaxResult(true));
        }
        /// <summary>
        /// 根据id查询数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("EditUserInfoid")]
        [HttpPost]
        public async Task<IActionResult> EditUserInfoid([FromBody] UserInfoDto id)
        {
            UserInfos user = await _repository.FirstOrDefaultAsync(s => s.Id == id.id);
            if (user == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(DataResult(user));
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="userInfoDto"></param>
        /// <returns></returns>
        [Route("EditUserInfo")]
        [HttpPost]
        public async Task<IActionResult> EditUserInfo(UserInfoDto userInfoDto)
        {
            UserInfos user = await _repository.UpdateAsync(_mapper.Map<UserInfos>(userInfoDto));
            if (user == null)
            {
                return Json(AjaxResult(false));
            }
            return Json(AjaxResult(true));
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("DelUserInfo")]
        [HttpDelete]
        public async Task<IActionResult> DelUserInfo(Guid id)
        {
            await _repository.DeleteAsync(s => s.Id == id);
            return Json(AjaxResult(true));
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("IsLock")]
        [HttpPost]
        public async Task<IActionResult> IsLock(Guid id)
        {
            UserInfos the = await _repository.FirstOrDefaultAsync(p => p.Id == id);
            the.UserName = the.UserName;
            the.UserPwd = the.UserPwd;
            if (the.UserState == 0)
            {
                the.UserState = 1;
            }
            else if(the.UserState==1)
            {
                the.UserState = 0;
            }
            else
            {
                the.UserState = the.UserState;
            }
            the.UserState = the.UserState;
            the.Creator = the.Creator;
            the.CreateTime = the.CreateTime;            
            the.IsDeleted =the.IsDeleted;
            await _repository.UpdateAsync(the);
            return Ok(Success("修改成功"));
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("UpdatePwd")]
        [HttpPost]
        public async Task<IActionResult> UpdatePwd(Guid id)
        {
            UserInfos the = await _repository.FirstOrDefaultAsync(p => p.Id == id);
            the.UserName = the.UserName;
            the.UserPwd = "123456";
            the.UserState = 0;
            the.Creator = the.Creator;
            the.CreateTime = the.CreateTime;
            the.IsDeleted = 0;
             await _repository.UpdateAsync(the);
            return Ok(Success("重置成功"));
        }
        /// <summary>
        /// 获取权限树
        /// </summary>
        /// <returns></returns>
        [Route("UserTree")]
        [HttpPost]
        public object UserTree([FromBody] UserInfoDto id)
        {

            //var list = _userInfoService.GetUserTree(id);
            //获取角色
            var list = _repositoryRole.GetAllList().Select(p => new
            {
                Id = p.Id,
                RoleName = p.RoleName,
                Remark = p.Remark,
                RoleState = p.RoleState,
                Creator = p.Creator,
                CreateTime = p.CreateTime

            }).ToList();
            //获取该用户的角色
            var therolelist=  _context.UserRoleInfos.Include(p => p.UserInfos).Include(p => p.RoleInfos).Where(x => x.UserInfosId == id.id).ToList();

            //var therolelist = _repositoryuserRole.GetAllList().Where(x => x.UserInfosId == id.id).ToList();
           // List<string> r = new List<string>();
            List<QuanXianDto> cd = new List<QuanXianDto>();
            foreach (var item in therolelist)
            {
                var cd1 = new QuanXianDto
                {
                    id = item.UserInfos.Id,
                    Name = item.UserInfos.UserName,
                    Children = new List<RoleInfoDtos>()
                };
                cd1.Children.Add(
                    new RoleInfoDtos { 
                    RoleName=item.RoleInfos.RoleName
                    }
                    );
                cd.Add(cd1);
            }
            return Ok(cd);
          
        }
        /// <summary>
        /// 获取角色名字
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("GetRoleName")]
        [HttpPost]
        public async Task<ActionResult> GetRoleName()
        {
            var the =await _repositoryRole.GetAllListAsync();
            
            List<RoleInfos> list = new List<RoleInfos>();
            foreach (var item in the)
            {

                var rolename = new RoleInfos {
                    Id=item.Id,
                    RoleName = item.RoleName
                };
                list.Add(rolename);
            }
            return Ok(list);
        }
    }
}
